﻿using LightingPlatform.Chaneels;
using LightingPlatform.Chaneels.ServiceReference;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Browser;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace LightingPlatform.ViewModel
{
    public class WebServiceProxy
    {
        static WebServiceProxy()
        {
            WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
            WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
        }

        private static Services _webService;

        private static Services WebService
        {
            get
            {
                if(_webService == null)
                {
                    _webService = new Services();

                    var binding = new BasicHttpBinding()
                    {
                        MaxBufferSize = int.MaxValue,
                        MaxReceivedMessageSize = int.MaxValue,
                        SendTimeout = TimeSpan.FromMinutes(10),
                        ReceiveTimeout = TimeSpan.FromMinutes(10),
                    };

                    EndpointAddress point = new EndpointAddress(BusinessContext.Singleton.WebServiceUrl);
                    _webService.LightingService = new WebServiceSoapClient(binding, point);
                }
                return _webService;
            }
        }

        public async Task<ResArray> GetStaticResource()
        {
            GetStaticResourceRequest request = new GetStaticResourceRequest(new GetStaticResourceRequestBody());
            GetStaticResourceResponse response = await WebService.GetStaticResource(request);
            return ResArray.ParseFrom(response.Body.GetStaticResourceResult);
        }

        public async Task<string> SearchProdName()
        {
            SearchProdNameRequest request = new SearchProdNameRequest(new SearchProdNameRequestBody());
            SearchProdNameResponse response = await WebService.SearchProdName(request);

            return response.Body.SearchProdNameResult;
        }

        public async Task<User> Login(string name, string pwd)
        {
            LoginRequest request = new LoginRequest(new LoginRequestBody(name, pwd));
            LoginResponse response = await WebService.Login(request);
            byte[] array = response.Body.LoginResult;
            if (array != null)
                return User.ParseFrom(array);

            return null;
        }

        public async Task<ProdViewArray> SearchProdList(byte[] conditions)
        {
            SearchProdListRequest request = new SearchProdListRequest(new SearchProdListRequestBody( conditions));
            SearchProdListResponse response = await WebService.SearchProdList(request);
            return ProdViewArray.ParseFrom(response.Body.SearchProdListResult);
        }

        public async Task<UserArray> GetUsers()
        {
            GetUsersRequest request = new GetUsersRequest(new GetUsersRequestBody());
            GetUsersResponse response = await WebService.GetUsers(request);
            return UserArray.ParseFrom(response.Body.GetUsersResult);
        }

        public async Task<int> AddUser(byte[] bytes)
        {
            AddUserRequest request = new AddUserRequest(new AddUserRequestBody(bytes));
            AddUserResponse response = await WebService.AddUser(request);
            return response.Body.AddUserResult;
        }

        public async Task<bool> UpdateUser(byte[] bytes)
        {
            UpdateUserRequest request = new UpdateUserRequest(new UpdateUserRequestBody(bytes));
            UpdateUserResponse response = await WebService.UpdateUser(request);
            return response.Body.UpdateUserResult;
        }

        public async Task<bool> DeleteUser(int id)
        {
            return await WebService.DeleteUser(id);
        }

        public async Task<int> AddProd(byte[] bytes)
        {
            AddProdRequest request = new AddProdRequest(new AddProdRequestBody(bytes));
            AddProdResponse response = await WebService.AddProd(request);
            return response.Body.AddProdResult;
        }

        public async Task<bool> UpdateProd(byte[] bytes)
        {
            UpdateProdRequest request = new UpdateProdRequest(new UpdateProdRequestBody(bytes));
            UpdateProdResponse response = await WebService.UpdateProd(request);
            return response.Body.UpdateProdResult;
        }

        public async Task<bool> DeleteProd(int id)
        {
            return await WebService.DeleteProd(id);
        }

        public async Task<ProdClassArray> SearchProdClass(byte[] conditions)
        {
            SearchProdClassRequest request = new SearchProdClassRequest(new SearchProdClassRequestBody(conditions));
            SearchProdClassResponse response = await WebService.SearchProdClass(request);
            return ProdClassArray.ParseFrom(response.Body.SearchProdClassResult);
        }

        public async Task<ProdViewArray> SearchProdSeries(byte[] conditions)
        {
            SearchProdSeriesRequest request = new SearchProdSeriesRequest(new SearchProdSeriesRequestBody( conditions));
            SearchProdSeriesResponse response = await WebService.SearchProdSeries(request);
            return ProdViewArray.ParseFrom(response.Body.SearchProdSeriesResult);
        }

        public async Task<PrimaryProd> SearchPrimaryProdInfo(int id)
        {
            SearchPrimaryProdInfoRequest request = new SearchPrimaryProdInfoRequest(new SearchPrimaryProdInfoRequestBody(id));
            SearchPrimaryProdInfoResponse response = await WebService.SearchPrimaryProdInfo(request);
            return PrimaryProd.ParseFrom(response.Body.SearchPrimaryProdInfoResult);
        }

        public async Task<Prod> SearchProdInfo(int id)
        {
            SearchProdInfoRequest request = new SearchProdInfoRequest(new SearchProdInfoRequestBody(id));
            SearchProdInfoResponse response = await WebService.SearchProdInfo(request);
            return Prod.ParseFrom(response.Body.SearchProdInfoResult);
        }

        public async Task<ProdClassArray> SearchRecommendedProd(byte[] conditions)
        {
            return null;
        }
    }
}
